package com.dwxt.sport.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.IntegralCategory;
import com.dwxt.sport.entity.RunRecord;
import com.dwxt.sport.service.feign.UserService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TaskService {
    @Autowired
    private RunRecordService runRecordService;

    @Autowired
    private UserService userService;


    public void addIntegral() {
        //获取一卡路里多少积分
        BaseResult baseResult = userService.selectRuleByCategory(IntegralCategory.RUN);
        int integral = JSONObject.fromObject(baseResult.getData()).getInt("integral");

        List<RunRecord> runRecordList = runRecordService.selectRecordToday();
        for (RunRecord runRecord : runRecordList) {
            try {
                //100kcal 100积分
                int total = (int) Math.floor(runRecord.getCalories()) / 1000 / 100 * integral;
                if (total > integral * 5) {
                    total = integral * 5;
                }
                userService.updateIntegral(total, null, IntegralCategory.RUN, runRecord.getUserId());
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }
}
